package com.lee.bemorebetter.degin.principle;

import java.util.*;

/**
 * @author ：liylw
 * @date ：Created in 2021/3/25 15:35
 */
public class EmailReporter {
    private static final Long DAY_HOURS_IN_SECONDS = 24*60*60L;

    private MetricsStorage metricsStorage;
    private EmailSender emailSender;
    private List<String> toAddresses = new ArrayList<>();

    public EmailReporter(MetricsStorage metricsStorage) {
       this(metricsStorage,new EmailSender());
    }
    public void addToAddress(String address){
        toAddresses.add(address);
    }

    public EmailReporter(MetricsStorage metricsStorage, EmailSender emailSender) {
        this.metricsStorage = metricsStorage;
        this.emailSender = emailSender;
    }




    public void startDailyReport(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        Date time = calendar.getTime();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                long durationInMillis = DAY_HOURS_IN_SECONDS*1000;
                long endTimeInMillis = System.currentTimeMillis();
                long startTimeInMillis = endTimeInMillis - durationInMillis;
                Map<String, List<RequestInfo>> requestInfos = metricsStorage.getRequestInfos(startTimeInMillis, endTimeInMillis);
                Map<String,RequestStat> stats = new HashMap<>();
                for (Map.Entry<String, List<RequestInfo>> entry : requestInfos.entrySet()) {
                    String apiName = entry.getKey();
                    List<RequestInfo> info = entry.getValue();
                    stats.put(apiName,Aggregator.aggregate(info,durationInMillis));
                }
            }
        }, time, DAY_HOURS_IN_SECONDS * 1000);
    }


    public static class EmailSender{

    }
}
